package com.apexsoft.monitor.front.console.userlog.controller;

import com.alibaba.fastjson.JSONObject;
import com.apexsoft.front.common.support.BaseFrontController;
import com.apexsoft.front.common.support.Result;
import com.apexsoft.front.common.utils.JsonResult;
import com.apexsoft.monitor.front.console.userlog.om.UserLog;
import com.apexsoft.monitor.front.console.userlog.service.UserLogService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * @author lsj
 *
 */
@Controller("userLogController")
@RequestMapping("/console/userLog")
public class UserLogController extends BaseFrontController {
	protected final transient Logger log = LogManager.getRootLogger();
	@Autowired
	@Qualifier("userLogService")
	private UserLogService userLogService = null;
	
	@RequestMapping(value = "/list")
	public String list(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		return "console/userLog/list";
	}
	
	@ResponseBody
	@RequestMapping(value="/logData")
	public void logData(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception
	{
		String key = request.getParameter("key");
		if(key!=null)key=key.trim();
		JSONObject result = userLogService.queryUserLogByPage(key);
		this.printJSON(response, result);
	}
	
	@ResponseBody
	@RequestMapping(value = "/detailView/{id}")
	public void detailView(@PathVariable String id,HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		
		JSONObject result = new JSONObject();
		UserLog tLog = userLogService.queryDetail(Integer.parseInt(id));
		if(tLog!=null){
			result.put("success", true);
			result.put("data", tLog);
		}else{
			result.put("success", false);
		}
		System.out.println(result.toJSONString());
		this.printJSON(response, result);
	}

	@ResponseBody
	@RequestMapping(value = "/listByPageForEasyui")
	public void listByPageForEasyui (HttpServletRequest request, HttpServletResponse response,  UserLog userLog) throws Exception {
		String pageStr = request.getParameter("pageNo");
		String pageCountStr = request.getParameter(Result.PAGE_SIZE);
		String remake = request.getParameter("remake");
		String userName = request.getParameter("userName");
		String ip = request.getParameter("ip");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");

		int page = 0, pageCount = 0;
		JSONObject result = new JSONObject();
		try {
			page = Integer.valueOf(pageStr);
			pageCount = Integer.valueOf(pageCountStr);
			result = userLogService.listByPageForEasyui(page, pageCount,remake, userName ,ip, startTime, endTime).getJSONObject("obj");
		} catch (Exception e) {
			log.error(e.getMessage());
			result = new JsonResult().getExceptionResult(e);
		}
		this.printJSON(response, result);
	}
}
